/*
* 用户有关访问接口数据
*/
import { setCommonSystemLoading, setCommonDate } from './Action';
import { hashHistory } from 'react-router';
import * as DataName from './DataNameConfig';
import { USER_API_TYPE } from "../utils/Api";
import {message} from 'antd';
import Util from '../utils/Util';
import HttpServer from '../axios';

export const SaveLoginUser=(user)=>{
    return {
        type:"loginSuccess",
        UserJson:user
    }
}

/* 退出登录 */
export const LoginOut=()=>{
    return{
        type:"loginOut",
    }
}

/**
 * 用户登录
 */
export const UserLogin =(values)=> {
    return dispatch=>{
        HttpServer(Util.getApi(USER_API_TYPE,"Login"),values)
            .then((res)=>{
                message.success("登录成功");
                res.result.token = res.token;
                dispatch(SaveLoginUser(res.result));// 保存登录信息
                hashHistory.push("/")
            },(error)=>{
                dispatch(setCommonSystemLoading(DataName.FORM_SPIN_LOADING,false));
                console.log(error)
            })
        /*Tool.ajaxCommon(Util.getApi(USER_API_TYPE,"Login"),JSON.stringify(values),
            (res) => {
                if(res.result){
                    res.result.token = res.token;
                    /!*var user = res.result.saasUser;
                    user.token = res.token;
                    user.saasOrg = res.result.saasOrg;*!/
                    dispatch(SaveLoginUser(res.result));// 保存登录信息
                    hashHistory.push("/")
                }else{
                    message.error(res.serror.desc);
                }
                setTimeout(function () {
                    dispatch(setCommonSystemLoading(DataName.FORM_SPIN_LOADING,false));
                },500)
            },(err) => {
                console.log(err);
            });*/
    }
}

/**
 * 忘记密码
 */
export const ForgetPasswordUser=(values)=>{
    return dispatch=>{
        /*Tool.ajaxCommon(Util.getApi(USER_API_TYPE,"ForgetPassword"),JSON.stringify(values),
            (res) => {
                if (res.result) {
                    message.success("密码修改成功");
                    dispatch(setCommonSystemLoading(DataName.BASE_MODAL,false));
                    dispatch(CountDown(0,"发送验证码"));
                }else{
                    message.error(res.serror.desc)
                }
                setTimeout(function () {
                    dispatch(setCommonSystemLoading(DataName.BASE_SPIN_LOADING,false));
                },500)
            }, (err) => {
                console.log(err);
            });*/
    }
}

/**
 * 修改密码
 */
export const EditPasswordUser=(type,values)=>{
    return dispatch=>{
        /*Tool.ajaxCommon(Util.getApi(USER_API_TYPE,"EditPassword"),JSON.stringify(values),
            (res) => {
                if (res.result) {
                    dispatch(LoginOut());
                    hashHistory.push("/login");
                    dispatch(setCommonSystemLoading(DataName.EDIT_PASSWORD_MODAL,false));
                    message.success("密码修改成功");
                }else{
                    message.error(res.serror.desc)
                }
                setTimeout(function () {
                    dispatch(setCommonSystemLoading(DataName.EDIT_PASSWORD_SPIN,false));
                },500)
            }, (err) => {
                console.log(err);
            });*/
    }
}

/**
 * 发送验证码
 */
export const SendCodeUser=(values)=>{
    return dispatch=>{
        HttpServer(Util.getApi(USER_API_TYPE,"SendCode"),values)
            .then((res)=>{
                message.success("短信发送成功");
                dispatch(CountDown(61));
            },(error)=>{
                message.info("短信发送失败");
            })
    }
}
let tim;
export const CountDown=(time)=>{
    return dispatch=>{
        let config = {...DataName.CODE_CONFIG_INFO};
        window.clearInterval(tim);
        if(time === 0){
            dispatch(setCommonDate("set_base_data",DataName.CODE_CONFIG,{...config}))
            return false;
        }
        tim = setInterval(()=>{
            time--;
            if(time<1){
                window.clearInterval(tim);
                config = {send:"重新发送",activeSend:"",sendCodeFlag:true}
            }else{
                config = {send:time+"s",activeSend:"active",sendCodeFlag:false};
            }
            dispatch(setCommonDate("set_base_data",DataName.CODE_CONFIG,{...config}))
        },1000);
    }
}

/**
 * 用户列表
 * @param {object} filter
 */
export const GetUserList =(filter)=> {
    return dispatch=>{
        let obj = {...Util.getApi(USER_API_TYPE,"UserList")};
        obj.url += filter.pageNo+"/"+filter.pageSize+"/"+filter.type;
        HttpServer(obj,filter)
            .then((res)=>{
                dispatch(setCommonSystemLoading(DataName.BASE_CARD_LOADING,false));
                dispatch(setCommonSystemLoading(DataName.BASE_TABLE_LOADING,false));
                dispatch(setCommonDate('set_base_data',DataName.USER_LIST,res.result))
            },(error)=>{
                dispatch(setCommonSystemLoading(DataName.BASE_TABLE_LOADING,false));
                console.log(error)
            })
    }
}
